package com.example.demo_job.service.impl;



import com.example.demo_job.entity.*;
import com.example.demo_job.mapper.PositionMapper;
import com.example.demo_job.mapper.RecruitMapper;
import com.example.demo_job.mapper.SeekerMapper;
import com.example.demo_job.service.SeekerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


@Service
public class SeekerServiceimpl implements SeekerService {
    @Autowired
    private SeekerMapper seekermapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private RecruitMapper recruitMapper;

    @Override
    public int register(String susername, String sname, String spassword, int sedu, String sgender, String sphone) {
        String sinfo=susername+".txt";
//        File txtfile=new File("C:\\Users\\22263\\Desktop\\大学相关\\java\\idea\\demo_job\\src\\main\\resources\\static\\SeekerTxt\\"+sinfo);
        File txtfile=new File("src\\main\\resources\\static\\SeekerTxt\\"+sinfo);
        try {
            txtfile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return seekermapper.register(susername,sname,spassword,sedu,sgender,sphone,sinfo);
    }

    @Override
    public Seeker findByUsername(String susername) {
        return seekermapper.findByUsername(susername);
    }

    @Override
    public Seeker login(String susername, String spassword) {
        return seekermapper.login(susername,spassword);
    }

    @Override
    public Recruit applyPosition(String susername,int pid) {
        int sid=seekermapper.findid(susername);
        Recruit recruit=recruitMapper.findBySidPid(sid,pid);
        if(recruit==null) recruitMapper.add(sid,pid);
        return recruit;
    }

    @Override
    public Recruit cancelApplyPosition(String susername,int pid) {
        int sid=seekermapper.findid(susername);
        Recruit recruit=recruitMapper.findBySidPid(sid,pid);
        if(recruit!=null) recruitMapper.delete(sid,pid);
        return recruit;
    }

    @Override
    public List<Position> findPositionByHot() {
        List<Position> positionByHot = positionMapper.findPositionByHot();
        //排序
        Collections.sort(positionByHot, new Comparator<Position>() {
            @Override
            public int compare(Position o1, Position o2) {
                return o2.getHot() - o1.getHot();
            }
        });
        //输出前十名
        if (positionByHot.size() >= 10) {
            List<Position> topTenPositions = positionByHot.subList(0, 10);
            return topTenPositions;
        }
        return positionByHot;
    }


    @Override
    public String findPositionPath(int cid, String pname) {
        return positionMapper.findPositionPath(cid,pname);
    }

    @Override
    public String findSinfo(String susername) {
        String info=seekermapper.findinfo(susername);
        if(info==null) return null;
//        String Tinfo="C:\\Users\\22263\\Desktop\\大学相关\\java\\idea\\demo_job\\src\\main\\resources\\static\\SeekerTxt\\"+info;
//        String Tinfo="src\\main\\resources\\static\\SeekerTxt\\"+info;
        String Tinfo="src/main/resources/static/SeekerTxt/SeekerTxt/"+info;
        return Tinfo;
    }

    @Override
    public void write(String tsinfo,String cv) {
        //tsinfo绝对路径，cv文本内容
        // 创建文件输出流
        BufferedWriter writer = null;
        // 获取绝对路径
        try {
            writer = new BufferedWriter(new FileWriter(tsinfo));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 将文本数据写入文件
        try {
            writer.write(cv);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 关闭文件输出流
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<ComPosition> searchForPositionByPos(String keyword) {
        String pname="%"+keyword+"%";
        List<Position> positionList=seekermapper.findPositionByPos(pname);
        List<ComPosition> comPositionList=new ArrayList<ComPosition>();
        for(Position position:positionList){
            int cid = position.getCid();
            Company company=seekermapper.findCompanyBycid(cid);
            ComPosition comPosition=new ComPosition(position,company);
            comPositionList.add(comPosition);
        }
        return comPositionList;
    }

    @Override
    public List<OneComMulPosition> searchForPositionByCom(String keyword) {
        String cname="%"+keyword+"%";
        List<Company> companyList=seekermapper.findCompanyBycname(cname);
        List<OneComMulPosition> OneComMulPositionList=new ArrayList<OneComMulPosition>();
        //容错，找不到公司
        if(companyList.size()==0) return OneComMulPositionList;
        for(Company company:companyList){
            int cid = company.getCid();
            List<Position> positionList=seekermapper.findPositionBycid(cid);
            //没有发布岗位，录入
            if(positionList.size()==0){
                OneComMulPosition oneComMulPosition=new OneComMulPosition(company);
                OneComMulPositionList.add(oneComMulPosition);
                continue;
            }
            OneComMulPosition oneComMulPosition=new OneComMulPosition(company,positionList);
            OneComMulPositionList.add(oneComMulPosition);
        }
        return OneComMulPositionList;
    }

    @Override
    public List<ComPosition> findComPositionByHot(List<Position> positionByHot) {
        List<ComPosition> comPositionList=new ArrayList<ComPosition>();
        for(Position position:positionByHot){
            int cid = position.getCid();
            Company company=seekermapper.findCompanyBycid(cid);
            ComPosition comPosition=new ComPosition(position,company);
            comPositionList.add(comPosition);
        }
        return comPositionList;
    }

    @Override
    public void revisePassword(int sid, String newPassword) {
        seekermapper.updatePassword(sid,newPassword);
    }

    @Override
    public List<ComPosition> showApplyComPosition(int sid) {
        List<Recruit> recruitList =seekermapper.findRecruitBysid(sid);
        List<ComPosition> comPositionList=new ArrayList<ComPosition>();
        for(Recruit recruit:recruitList){
            int pid=recruit.getPid();
            //先找职位表
            Position position=seekermapper.findPositionBypid(pid);
            //再找公司表
            int cid=position.getCid();
            Company company=seekermapper.findCompanyBycid(cid);
            //整合
            ComPosition comPosition=new ComPosition(position,company);
            comPosition.setFlag(recruit.isFlag());
            comPositionList.add(comPosition);
        }
        return comPositionList;
    }

    @Override
    public int findSeekerSidBySusername(String susername) {
        return seekermapper.findSeekerSidBySusername(susername);
    }

    @Override
    public String showSeekerCv(String sinfo) {
        //最终路径
//        String absolutePath="C:\\Users\\22263\\Desktop\\大学相关\\java\\idea\\demo_job\\src\\main\\resources\\static\\SeekerTxt\\"+sinfo;
        //        String absolutePath="src\\main\\resources\\static\\SeekerTxt\\"+sinfo;
        Resource resource=new ClassPathResource("static/SeekerTxt/"+sinfo);
        BufferedReader reader = null;
        String total_line = null;
        try {

//            reader = new BufferedReader(new FileReader(absolutePath));
            reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (total_line != null) {
                    total_line += "\n" + line; // 在每行前面添加一个换行符
                } else {
                    total_line = line; // 如果是第一行，直接赋值
                }
            }
        } catch (IOException e) {
            e.printStackTrace(); // 在调试阶段可以使用，生产环境中应更妥善处理异常
        } finally {
            if (reader != null) {
                try {
                    reader.close(); // 关闭BufferedReader
                } catch (IOException e) {
                    e.printStackTrace(); // 生产环境中应更妥善处理异常
                }
            }
        }
        return total_line;
    }

    @Override
    public Recruit judgeApplyPosition(String susername, int pid) {
        int sid=seekermapper.findid(susername);
        Recruit recruit=recruitMapper.findBySidPid(sid,pid);
        return recruit;
    }

    @Override
    public List<Company> showAllCompany() {
        return seekermapper.getCompanyList();
    }

    @Override
    public List<OneComMulPosition> showAllCompanyPosition(List<Company> companyList) {
        List<OneComMulPosition> oneComMulPositionList=new ArrayList<OneComMulPosition>();
        for(Company company:companyList){
            int cid = company.getCid();
            List<Position> positionList=seekermapper.findPositionBycid(cid);
            if(positionList.size()==0){
                OneComMulPosition oneComMulPosition=new OneComMulPosition(company);
                oneComMulPositionList.add(oneComMulPosition);
                continue;
            }
            OneComMulPosition oneComMulPosition=new OneComMulPosition(company,positionList);
            oneComMulPositionList.add(oneComMulPosition);
        }
        return oneComMulPositionList;
    }

    @Override
    public List<Position> showPosition() {
        return seekermapper.showPosition();
    }

    @Override
    public List<OnePositionMulCompany> PositionClassify(List<Position> positionList) {
        List<OnePositionMulCompany> onePositionMulCompanyList=new ArrayList<OnePositionMulCompany>();
        for (Position position : positionList){
            boolean con=true;
            if(onePositionMulCompanyList.size()==0){
                int cid = position.getCid();
                Company company=seekermapper.findCompanyBycid(cid);
                ComPosition comPosition=new ComPosition(position,company);
                OnePositionMulCompany onePositionMulCompany=new OnePositionMulCompany(position,comPosition);
                onePositionMulCompanyList.add(onePositionMulCompany);
                continue;
            }
            for(OnePositionMulCompany onePositionMulCompany :onePositionMulCompanyList){
                //职位同名，不同公司
                if(onePositionMulCompany.getPname().equals(position.getPname())){
                    con=false;
                    Company company=seekermapper.findCompanyBycid(position.getCid());
                    ComPosition comPosition=new ComPosition(position,company);
                    onePositionMulCompany.getComPositionList().add(comPosition);
                    break;
                }
            }
            //职位和公司都未出现过
            if(con){
                int cid = position.getCid();
                Company company=seekermapper.findCompanyBycid(cid);
                ComPosition comPosition=new ComPosition(position,company);
                OnePositionMulCompany onePositionMulCompany=new OnePositionMulCompany(position,comPosition);
                onePositionMulCompanyList.add(onePositionMulCompany);
            }
        }
        return onePositionMulCompanyList;
    }

    @Override
    public Seeker findSeekerBysid(int sid) {
        return seekermapper.findSeekerBysid(sid);
    }
}
